package swg.shape;

public class Branch implements IswgShape
{
	protected Point start;
	protected Point end;
	protected Point zero;

	protected Color styleColor;
	protected int styleStrike = 0;
	protected boolean caps;
	protected int styleCapsRadius;
	protected Color styleCapsColor;

	protected String txt="";	
	
	
	public String getTxt()
	{
		return txt;
	}

	public void setTxt(String txt)
	{
		this.txt = txt;
	}
	
	public Branch(Point start, Point end)
	{
		this.start = start;
		this.end = end;
		zero = new Point(end.getX() - start.getX(), end.getY() - start.getY());
	}
	
	public Point getStart()
	{
		return start;
	}



	public void setStart(Point start)
	{
		this.start = start;
	}



	public Point getEnd()
	{
		return end;
	}



	public void setEnd(Point end)
	{
		this.end = end;
	}



	public Point getZero()
	{
		return zero;
	}



	public void setZero(Point zero)
	{
		this.zero = zero;
	}



	public Color getStyleColor()
	{
		return styleColor;
	}



	public void setStyleColor(Color styleColor)
	{
		this.styleColor = styleColor;
	}



	public int getStyleStrike()
	{
		return styleStrike;
	}



	public void setStyleStrike(int styleStrike)
	{
		this.styleStrike = styleStrike;
	}



	public boolean isCaps()
	{
		return caps;
	}



	public void setCaps(boolean caps)
	{
		this.caps = caps;
	}



	public int getStyleCapsRadius()
	{
		return styleCapsRadius;
	}



	public void setStyleCapsRadius(int styleCapsRadius)
	{
		this.styleCapsRadius = styleCapsRadius;
	}



	public Color getStyleCapsColor()
	{
		return styleCapsColor;
	}



	public void setStyleCapsColor(Color styleCapsColor)
	{
		this.styleCapsColor = styleCapsColor;
	}



	public double getPhi()
	{
		return zero.getPhi();
	}

	public double getRho()
	{
		return zero.getRho();
	}

	@Override
	public int getR()
	{
		double res = Math.sqrt(
		(start.getX()-end.getX())*(start.getX()-end.getX())+
		(start.getY()-end.getY())*(start.getY()-end.getY()));
		return (int) res;
	}

	@Override
	public String getSWGCode()
	{
		StringBuffer swg = new StringBuffer("\n<line ");
		// <line x1="0" y1="0" x2="200" y2="200"
		// style="stroke:rgb(255,0,0);stroke-width:2"/>
		swg.append(" x1 =\"" + start.getX() + "\"");
		swg.append(" y1 =\"" + start.getY() + "\"");
		swg.append(" x2 =\"" + end.getX() + "\"");
		swg.append(" y2 =\"" + end.getY() + "\"");
		String style="";
		if (styleColor != null)
		{
			style=style+" stroke:" + styleColor.getSWGCode()+"; ";
		}
		if (styleStrike != 0)
		{
			style=style+" stroke-width:" + styleStrike;
		}
		if(style.length()==0){
			style="stroke:rgb(0,0,0) ;  stroke-width:2";
		}

		swg.append(" style=\""+style+"\" />");
		if (caps)
		{
			Circle sc= new Circle(start.getX(), start.getY(), styleCapsRadius,0, "", styleCapsColor.getSWGCode());
			swg.append(sc.getSWGCode());

			Circle se= new Circle(end.getX(), end.getY(), styleCapsRadius,0, "", styleCapsColor.getSWGCode());
			swg.append(se.getSWGCode());
		}
		if(txt.length()>0){
			Text t = new Text(end.getX(), getY(), txt);
			swg.append(t.getSWGCode());
		}
		return swg.toString();
	}

	@Override
	public int getX()
	{
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getY()
	{
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String toString()
	{
		return "Branch [end=" + end + ", start=" + start + ", zero=" + zero + "]";
	}
	
	public Point getMiddlePoint(double direction){
		Point res = null;
		
		Branch translate = this.Transalte(end.getX()-start.getX(),0);
		int xx =(int)(translate.getStart().getX()+direction*(translate.getEnd().getX()-translate.getStart().getX())/2); 
		int yy=(int) (translate.getStart().getY()+direction*(translate.getEnd().getY()-translate.getStart().getY())/2);
		res= new Point(xx,yy);
				
		return res;
	}

	public Branch Transalte(int x, int y)
	{
		Point startN = this.start.Translate(x,y);
		Point endN = this.end.Translate(x,y);
		Branch res = new Branch(startN, endN);
		return res;
	}
	
}
